package com.dmooo.yxmmerchant.view.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.RecycleViewDivider;
import com.common.util.SPUtils;
import com.common.util.StartActivityUtil;
import com.common.util.ToastUtil;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.adapter.MerchantAdminListAdapter;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.domain.MerchantAdminListBean;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SelKfActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rg_mall)
    RadioGroup rgMall;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private List<MerchantAdminListBean.Item> list = new ArrayList<>();
    private MerchantAdminListAdapter merchantAdminListAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_sel_kf;
    }

    @Override
    protected void initEventAndData() {
        tvTitle.setText("转接客服选择");
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        merchantAdminListAdapter = new MerchantAdminListAdapter(R.layout.item_merchant_admin, list);
        recyclerview.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        recyclerview.setAdapter(merchantAdminListAdapter);
        getList();
        rgMall.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                getList();
            }
        });
        merchantAdminListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (getIntent().getExtras().getString("type") != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("name", list.get(position).truename);
                    bundle.putString("uuid", list.get(position).id);
                    bundle.putString("type","0");
                    StartActivityUtil.startActivity(SelKfActivity.this, ChatActivity.class, bundle);
                    finish();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("uuid", list.get(position).id);
                intent.putExtra("nick", list.get(position).truename);
                intent.putExtra("avatar", list.get(position).avatar + "");
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void getList() {
        Map<String, Object> map = new HashMap<>();
        map.put("merchant_id", merchant_id);
        map.put("p", 1);
        map.put("per", 50);
        map.put("type", rgMall.getCheckedRadioButtonId() == R.id.rb_one ? 1 : rgMall.getCheckedRadioButtonId() == R.id.rb_two ? 2 : rgMall.getCheckedRadioButtonId() == R.id.rb_three ? 3 : 4);
        Log.d("dsfasdfs", map.toString());
        SubjectPost post = new SubjectPost(new ProgressSubscriber(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                GsonBuilder builder = new GsonBuilder();
                builder.serializeNulls();
                list.clear();
                try {
                    list.addAll(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(), MerchantAdminListBean.class).list);
                    for (int i = 0; i < list.size(); i++) {
                        if (SPUtils.get(SelKfActivity.this, Config.ADMIN_ID, "").toString().equals(list.get(i).id)) {
                            list.remove(i);
                            break;
                        }
                    }
                    merchantAdminListAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(Object o) {
                ToastUtil.getInstance().showErrorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        }, this, true, false, "删除中"), map);
        HttpManager.getInstance().getServiceAdminList(post);
    }

    @Override
    protected void createPresenter() {

    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }
}
